package com.imy.virtual.helper.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import com.imy.virtual.client.core.VirtualCore;
import com.imy.virtual.client.stub.VASettings;

import java.util.List;

public class AppUtils {

  public static boolean check64SupportInstall(Context context) {
    PackageManager packageManager = context.getPackageManager();
    List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
    for (int i = 0; i < packageInfos.size(); i++) {
      PackageInfo packageInfo = packageInfos.get(i);
      //过滤掉系统app
      if ((ApplicationInfo.FLAG_SYSTEM & packageInfo.applicationInfo.flags) != 0) {
        continue;
      }
      if (packageInfo.packageName.equals(VASettings.SUPPORT_64_PKG)) {
        return true;
      }
    }
    return false;
  }

  public static List<PackageInfo> getSystemInstallPackage() {
    return VirtualCore.get().getUnHookPackageManager().getInstalledPackages(0);
  }

  public static PackageInfo isSystemInstallPackage( String packageName) {
    List<PackageInfo> systemInstallPackage = getSystemInstallPackage();
    for (int i = 0; i < systemInstallPackage.size(); i++) {
      String packageName1 = systemInstallPackage.get(i).packageName;
      if (packageName1.equals(packageName)) {
        return systemInstallPackage.get(i);
      }
    }
    return null;
  }

  public static String getPackageNameByName(String checkjstring) {
    List<PackageInfo> systemInstallPackage = getSystemInstallPackage();
    for (int i = 0; i < systemInstallPackage.size(); i++) {
      ApplicationInfo applicationInfo = systemInstallPackage.get(i).applicationInfo;
      String charSequence = (String) applicationInfo.loadLabel(VirtualCore.get().getUnHookPackageManager()).toString();
      if (charSequence.equals(checkjstring)){
        return systemInstallPackage.get(i).packageName;
      }
    }
    return "";
  }
}
